from PIL import Image
import os
from config import ICON_PNG_PATH, ICON_ICO_PATH, ICON_SIZE

def convert_png_to_ico():
    """将现有的PNG图标转换为ICO格式，支持多种尺寸"""
    try:
        if not os.path.exists(ICON_PNG_PATH):
            print(f"错误：PNG图标文件不存在: {ICON_PNG_PATH}")
            return False
            
        # 打开PNG图像
        image = Image.open(ICON_PNG_PATH)
        
        # 准备多个尺寸的图像
        sizes = [(16, 16), (32, 32), (48, 48), (64, 64), (128, 128), (256, 256)]
        images = []
        
        for size in sizes:
            # 调整大小并保持RGBA模式
            resized_image = image.resize(size, Image.Resampling.LANCZOS)
            if resized_image.mode != 'RGBA':
                resized_image = resized_image.convert('RGBA')
            images.append(resized_image)
        
        # 保存为ICO，包含所有尺寸
        images[0].save(ICON_ICO_PATH, "ICO", sizes=sizes, append_images=images[1:])
        print(f"ICO图标已保存到: {ICON_ICO_PATH}")
        return True
    except Exception as e:
        print(f"转换出错: {str(e)}")
        return False

if __name__ == "__main__":
    convert_png_to_ico() 